package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.krb5.internal.crypto.Des;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService  strategyDetailService;
    @Reference
    private ITravelService travelService;

    //查询全部目的地
    @RequestMapping("")
    public  String index(Model model){
        model.addAttribute("hotRegions", regionService.selectAll());
        return   "destination/index";
    }

    @RequestMapping("guide")
    public  String guide(Long id,Model model){
        //tosats
        List<Destination> tosats = destinationService.listToasts(id);

        //catalogs
        model.addAttribute("catalogs", strategyCatalogService.listByDestID(id));
        model.addAttribute("toasts",tosats);
        //dest{name:xx}
        model.addAttribute("dest",tosats.remove(tosats.size()-1));
        //strategyDetails
        model.addAttribute("strategyDetails",strategyDetailService.getGuideId(id));
        return  "destination/guide";
    }
    @RequestMapping("listHotDestByRegionId")
    public  String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model){
        List<Destination> dests=destinationService.listHotDestByRegionId(regionId);

        List<Destination> leftDests = dests.subList(0, dests.size() / 2);
        List<Destination> rightDests = dests.subList(dests.size() / 2, dests.size());
        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        return  "destination/hotdestTpl";
    }
    @RequestMapping("surveyPage")
    public  String getDetail(@ModelAttribute("qo") DestinationQueryObject qo, Model model){
        List<Destination> toasts = destinationService.listToasts(qo.getDestId());
        //toasts
        model.addAttribute("toasts",toasts);
        //dest (name:xx)
        model.addAttribute("dest",toasts.remove(toasts.size()-1));
        return "destination/survey";
    }
    //概况内容
    @RequestMapping("survey")
    public String survey(@ModelAttribute("qo")DestinationQueryObject qo ,Model modle){
        List<StrategyCatalog> catalogs = strategyCatalogService.listByDestID(qo.getDestId());
        StrategyCatalog catalog=null;

        for(StrategyCatalog strategyCatalog:catalogs){
            System.out.println(qo.getCatalogId());
            if(strategyCatalog.getId().equals(qo.getCatalogId())){
                catalog=strategyCatalog;
                break;
            }
        }
        Long id = catalog.getDetails().get(0).getId();
       StrategyDetail detail = strategyDetailService.selectByPrimaryKey(id);

        //catalogs
        modle.addAttribute("catalogs",catalogs);
        //catalog
        modle.addAttribute("catalog",catalog);
        //detail
        modle.addAttribute("detail",detail);
        return "destination/surveyTpl";
    }

    //查询点击量前3的游记
    @RequestMapping("travels")
    public  String travels(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        model.addAttribute("pageInfo",travelService.queryForList(qo));
        return "destination/travelTpl";
    }




}
